package user.controller;

import common.util.Res;
import common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import system.entity.SysUser;
import system.model.dto.UserForm;
import system.service.SysUserService;
import system.shiro.password.JesPasswordServiceImpl;
import system.shiro.utils.ShiroUtils;
import user.entity.User;
import user.service.UserService;

import java.util.List;
import java.util.UUID;

/**
 * @author Yang
 * @Date 2019/8/7 19:24
 */
@RestController
@RequestMapping("/Yang/User")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JesPasswordServiceImpl jesPasswordService;

    @Autowired
    private SysUserService sysUserService;

    /***
     * @author Yang
     * @date 2019/8/7 19:29
     * @return : common.util.Result
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<User> userList = userService.findAll();
        return Res.makeOKRsp(userList);
    }

    /***
     * @author Yang
     * @date 2019/8/7 19:30
     * @param user :
     * @return : common.util.Result
     */
    @PostMapping("/update")
    public Result update(User user){
        int update = userService.update(user);
        return Res.makeOKRsp(update);
    }

    /***
     * @author Yang
     * @date 2019/8/7 19:32
     * @param  :
     * @return : common.util.Result
     */
    @GetMapping("/findById")
    public Result findById(){
        final SysUser user = ShiroUtils.getUser();

        User byId = userService.findById(user.getId());
        return Res.makeOKRsp(byId);
    }


    /***
     * 注册
     * @author Yang
     * @date 2019/8/14 19:15
     * @param user :
     * @return : common.util.Result
     */
    @PostMapping("/register")
    public Result<Object> register(User user){
        String id = UUID.randomUUID().toString();
        user.setId(id);
        String password = jesPasswordService.IencryptPassword(user);
        user.setPassword(password);
        userService.register(user);
        return Res.makeOKRsp();
    }

    /***
     * 修改密码
     * @author Yang
     * @date 2019/8/27 20:47
     * @param userForm :
     * @return : common.util.Result
     */
    @PostMapping("/updatePassword")
    @Transactional
    public Result updatePassword(UserForm userForm){
        final SysUser user = ShiroUtils.getUser();
        this.sysUserService.changePwd(userForm, user.getId(), userForm.getPassword(), userForm.getNewpassword1());
        return Res.makeOKRsp();
    }

    /***
     * @author Yang
     * @date 2019/9/1 19:21
     * @param user :
     * @return : common.util.Result
     */
    @PostMapping("/forgetPwd")
    public Result forgetPwd(User user){
        String password = jesPasswordService.IencryptPassword(user);
        user.setPassword(password);
        userService.forgetPwd(password);
        return Res.makeOKRsp();
    }
}
